<?php
header('Content-Type:text/html;charset=utf-8');
/*
 * 获取密码锁密码
 */
$strLocked = $_GET['pink'];
$email = $_GET['email'];
//echo "<br>Client string=".$strLocked."<br>";
//echo "<br>Clent email=".$email."<br>";
if (empty($email)   ||  empty($strLocked))
{
    var_dump($email);
    var_dump($strLocked);
    exit;
}
$strServer = substr($strLocked, 16, 8).substr($strLocked, 32, 8)
    .substr($strLocked, 8, 8).substr($strLocked, 24, 8)
    .substr($strLocked, 48, 8).substr($strLocked, 0, 8)
    .substr($strLocked, 56, 8).substr($strLocked, 40, 8);
//echo "After server encrypted=".$strServer;

$mds = null;
$mds[0] = '95d565ef66e7dff9';
$mds[1] = 'a0b923820dcc509a';
$mds[2] = '9d4c2f636f067f89';
$mds[3] = '4b5ce2fe28308fd9';
$mds[4] = 'a2f3e71d9181a67b';
$mds[5] = 'bbce2345d7772b06';
$mds[6] = '5a880faf6fb5e608';
$mds[7] = 'ceea167a5a36dedd';
$mds[8] = 'fb98ab9159f51fd0';
$mds[9] = '2e2d7fbdea1afc51';

$sa = substr($strServer, 0, 16);
$sb = substr($strServer, 16, 16);
$sc = substr($strServer, 32, 16);
$sd = substr($strServer, 48, 16);

$n1 = getStringPosition($sa, $mds);
$n2 = getStringPosition($sb, $mds);
$n3 = getStringPosition($sc, $mds);
$n4 = getStringPosition($sd, $mds);

$body   =   "
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
    <title>粉粉日记密码锁</title>
</head>
<body>
    <p>$n1$n2$n3$n4</p>
    <p>请使用上面的数字开启密码锁，并在登陆后重置密码。</p>
    <p></p>
    <p>更多信息请参考：<br />粉粉日记官方网站：http://www.fenfenriji.com </p>
    <p></p>
    <p>本邮件是根据用户的请求自动发送，不接收任何邮件回复，有问题请联系上面的信息</p>
</body>
</html>";

$subject    =   '粉粉日记密码锁';
#   $header     =   "From: 粉粉日记<xxtstudio@163.com>";
#   $result     =   mail($email, $subject, $body, $header);
#   $url = 'https://sendcloud.sohu.com/webapi/mail.send.json';
#   #   不同于登录SendCloud站点的帐号，您需要登录后台创建发信域名，获得对应发信域名下的帐号和密码才可以进行邮件的发送。
#   $param  =   array(
#                   'api_user'  =>  'postmaster@pink.sendcloud.org',
#                   'api_key'   =>  'pXlP3yWF',
#                   'from'      =>  'mmcz@fenfenriji.com',
#                   'fromname'  =>  '粉粉日记',
#                   'to'        =>  $email,
#                   'subject'   =>  $subject,
#                   'html'      =>  $body
#               );
#
#   $options    =   array('http' => array('method'  => 'POST','content' => http_build_query($param)));
#   $context    =   stream_context_create($options);
#   $result     =   file_get_contents($url, false, $context);


# ------
require("/data0/wwwroot/www.fenfenriji.com/sns/lib/PHPMailer_5.2.10/PHPMailerAutoload.php");
$mail = new PHPMailer;                                    #   建立邮件发送类
# $address        = $_POST['address'];
$mail->IsSMTP();                                            #   使用SMTP方式发送
$mail->CharSet  =   'UTF-8';                                #   设置邮件的字符编码
$mail->Host     =   "smtp.exmail.qq.com";                  #   您的企业邮局域名
$mail->SMTPAuth =   TRUE;                                   #   启用SMTP验证功能
$mail->Username =   "mmcz@fenfenriji.com";                  #   邮局用户名(请填写完整的email地址)
$mail->Password =   "2013@xxT";                             #   邮局密码
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to
$mail->From = "mmcz@fenfenriji.com";
$mail->FromName = "粉粉日记";
//$mail->SetFrom("mmcz@fenfenriji.com", "粉粉日记");
$mail->AddAddress($email, NULL);                   #   收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
$mail->AddReplyTo("xxtstudio@163.com", "粉粉日记");
$mail->SMTPDebug  = 0;
# $mail->AddAttachment("/var/tmp/file.tar.gz");             #   添加附件
# $mail->isHTML(true); // set email format to HTML          #   是否使用HTML格式
$mail->Subject  =   $subject;                       #   邮件标题
$mail->Body     =   $body;                         #   邮件内容
# $mail->AltBody  =   "This is the body in plain text for non-HTML mail clients";    #   附加信息，可以省略
// $result =   $mail->Send();
$result = sendCloud(array('email' => $email, 'subject' => $subject, 'body' => $body));

logRecord(substr($body, 0, 4), $result);
//echo ' Send Email is ';
//var_dump(int($result));
if ($result)    echo 1;
else    echo 0;

function getStringPosition($parm, $array){
    $result = -1;
    $mds = null;
    $mds[0] = '95d565ef66e7dff9';
    $mds[1] = 'a0b923820dcc509a';
    $mds[2] = '9d4c2f636f067f89';
    $mds[3] = '4b5ce2fe28308fd9';
    $mds[4] = 'a2f3e71d9181a67b';
    $mds[5] = 'bbce2345d7772b06';
    $mds[6] = '5a880faf6fb5e608';
    $mds[7] = 'ceea167a5a36dedd';
    $mds[8] = 'fb98ab9159f51fd0';
    $mds[9] = '2e2d7fbdea1afc51';
    for($i=0;$i<10;$i++){
        if($mds[$i]==$parm){
            $result = $i;
            break;
        }
    }
    return $result;
}

function logRecord($body, $result)
{
    $string =   "\t ". date('Y-m-d H:i:s'). "\t email: ". $_REQUEST['email']. " \t pink: ". $_REQUEST['pink']. " \t Code: ". $body. " \t Result: ". $result. "\r\n";
    file_put_contents('/tmp/pinkCode.txt', $string, FILE_APPEND);

    $sql    =   " INSERT INTO xxtMessage.passwdLock_0(datetime, email, pink, code, result) VALUES ('". date('Y-m-d H:i:s'). "', '". $_REQUEST['email']. "', '". $_REQUEST['pink']. "', '". $body. "', '". $result. "')";

    // $sql    =   ' mysql -hFB -uffrjSNS -pFB@ffrj.217 -e"'. $sql. '"';
    $sql    =   ' mysql -hM2 -uffrjSNS -pFB@ffrj.217 -e"'. $sql. '"';

    //exec($sql);
}

function sendCloud ($param) {
    $url    = "http://sendcloud.sohu.com/webapi/mail.send.json";
    $data = array(
        'api_user'  =>  'postmaster@pink.sendcloud.org',
        'api_key'   =>  '3kkLz0bFbFSsJ16z',
        'from'      =>  'mmcz@fenfenriji.com',
        'fromname'  =>  '粉粉日记',
        'to'        =>  $param['email'],
        'subject'   =>  $param['subject'],
        'html'      =>  $param['body']
    );

    $json   = getHttpResponsePOST($url, $data);
    $result = json_decode($json, true);
    if ($result['message'] != 'success'){
        return false;
    }
    return true;
}

function getHttpResponsePOST($url, $para, $input_charset = '') {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, 0 ); // 过滤HTTP头
    curl_setopt($curl,CURLOPT_POST,true); // post传输数据
    curl_setopt($curl,CURLOPT_POSTFIELDS,$para);// post传输数据
    curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
    curl_setopt($curl, CURLOPT_USERAGENT, "FFWEB_1.0-API");
    curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_ENCODING, "");
    curl_setopt($curl, CURLOPT_HEADER, FALSE);
    $responseText = curl_exec($curl);

    // var_dump( curl_error($curl) );exit;//如果执行curl过程中出现异常，可打开此开关，以便查看异常内容
    curl_close($curl);
    return $responseText;
}